package com.liheng.yygh.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liheng.yygh.common.exception.YyghException;
import com.liheng.yygh.common.result.Result;
import com.liheng.yygh.common.utlis.MD5;
import com.liheng.yygh.hosp.service.HospitalSetService;
import com.liheng.yygh.model.hosp.HospitalSet;
import com.liheng.yygh.vo.hosp.HospitalQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * @program: yygh_partn
 * @description:
 * @author: _LiHeng
 * @create: 2022-12-18 17:28
 * 简单的允许跨域 :@CrossOrigin
 **/
@Api(tags="医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
@CrossOrigin
public class HospitalSetController {
    //http://localhost:8201/admin/hosp/hospitalSet/QueryHospitalSetList
    /**
     * 注入service
     */
    @Autowired
    private HospitalSetService hospitalSetService;

    /**
     * 1.查询医院设置表的所有信息
     * @return
     */
    @GetMapping("QueryHospitalSetList")
    @ApiOperation(value = "获取所有医院设置信息")
    public Result ueryHospitalSetList(){
        //调用Service的方法
        List<HospitalSet> hospitalSetList = hospitalSetService.list();
        Result<List<HospitalSet>> listResult = Result.ok(hospitalSetList);
        return listResult;
    }

    /**
     * 2.删除医院设置
     */
    @DeleteMapping("{id}")
    @ApiOperation("逻辑删除单个医院信息")
    @ApiParam(name = "id",value = "医院设置id",required = true)
    public Result removeHospitalSetId(@PathVariable Long id){
        boolean removeById = hospitalSetService.removeById(id);
        if (removeById) {
            return Result.ok();
        }else{
            return Result.fail();
        }
    }

    /**
     * 3.条件查询带分页医院设置信息
     */
    @PostMapping("findPageHospitalSet/{current}/{limit}")
    @ApiOperation("条件查询带分页医院设置信息")
    public Result findPageQueryHospitalSet(@PathVariable Long current,
                                           @PathVariable Long limit,
                                           @RequestBody(required = false) HospitalQueryVo hospitalQueryVo){
        //创建一个page对象,传递当前页,每页记录数
        Page<HospitalSet> page = new Page<>(current,limit);
        //使用QueryWrapper 条件构造器
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        //避免出现空值的情况出现,字段进行判断
        //医院名称
        String hosname = hospitalQueryVo.getHosname();
        //医院编码
        String hoscode = hospitalQueryVo.getHoscode();
        if (!StringUtils.isEmpty(hosname)) {
            queryWrapper.like("hosname",hospitalQueryVo.getHosname());
        }
        if (!StringUtils.isEmpty(hoscode)) {
            queryWrapper.eq("hoscode",hospitalQueryVo.getHoscode());
        }

        //调用方法实现分页查询
        Page<HospitalSet> hospitalSetPage = hospitalSetService.page(page, queryWrapper);
        return Result.ok(hospitalSetPage);
    }

    /**
     * 4.添加医院设置
     */
    @PostMapping("saveHospitalSet")
    @ApiOperation("添加医院设置")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet){
        //设置状态 1使用 0使用
        hospitalSet.setStatus(1);
        //生成签名秘钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));
        boolean save = hospitalSetService.save(hospitalSet);
        if (save) {
            return Result.ok();
        }else{
            return  Result.fail();
        }


    }


    /**
     * 5.根据id获取医院设置
     */
    @GetMapping("getHospSet/{id}")
    @ApiOperation("根据ID获取医院设置")
    public Result getHospSet(@PathVariable Long id){

//        try{
//            //模拟500报错
//            int i = 1/0;
//        }catch (Exception e){
//            throw new YyghException("失败",201);
//        }
        HospitalSet byId = hospitalSetService.getById(id);
        return Result.ok(byId);
    }

    /**
     * 6.修改医院设置
     */
    @PostMapping("updateHospSet")
    @ApiOperation("修改医院设置")
    public Result updatehospitalSet(@RequestBody HospitalSet hospitalSet){
        boolean updateById = hospitalSetService.updateById(hospitalSet);
        if (updateById){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }

    /**
     * 7.批量修改删除医院设置接口
     */
    @DeleteMapping("batchRemove")
    @ApiOperation("批量修改删除医院设置接口")
    public Result batchRemove(@RequestBody List<Long> idList ){
        hospitalSetService.removeByIds(idList);
        return Result.ok();
    }


    /**
     * 8.医院设置锁定和解锁
     * @return
     */
    @PutMapping("lookHospitalSet/{id}/{status}")
    @ApiOperation("医院设置锁定和解锁")
    public Result lockHospitalSet(@PathVariable Long id,
                                  @PathVariable Integer status){
        //根据id查询医院设置信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //设置医院锁定与解锁
        hospitalSet.setStatus(status);
        //调用更新方法
        boolean b = hospitalSetService.updateById(hospitalSet);
        if (b){
           return Result.ok();
        }else{
           return Result.fail();
        }
    }
    //9.发送签名秘钥
    @PutMapping("sendKey/{id}/{status}")
    @ApiOperation("发送签名秘钥")
    public Result sendKey(@PathVariable Long id,
                                  @PathVariable Integer status) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        //TODO 发送短信

        return Result.ok();
    }

}
